package com.xuexi.demos.service.impl;

import com.xuexi.demos.entity.AIDetectionResult;
import com.xuexi.demos.entity.DTO.AIDetectionResultDTO;
import com.xuexi.demos.service.StudentService;
import com.xuexi.demos.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void processDetectionResult(AIDetectionResultDTO dto) {
        List<AIDetectionResult> results = dto.getDetections().stream().map(item -> {
            AIDetectionResult result = new AIDetectionResult();
            result.setObjectKey(dto.getObjectKey());
            result.setClassName(item.getClassName());
            result.setConfidence(item.getConfidence());
            result.setInferenceTimeMs(dto.getInferenceTimeMs()); // ✅ 设置 inferenceTimeMs
            result.setDevice(dto.getDevice());
            result.setCreatedAt(LocalDateTime.ofInstant(
                    Instant.parse(dto.getTimestamp()),
                    ZoneId.systemDefault()
            ));
            return result;
        }).collect(Collectors.toList());

        studentMapper.batchInsert(results);
    }
}
